<?php
/**
 * Description of Accounts
 *
 * @author Gowork
 */
class Application_Model_Mapper_Accounts extends App_Mapper_Abstract {
    
    protected $_dbTableClass = 'Application_Model_DbTable_Account';
           
    /**
     * Pobiera dane konto
     * @param int $id
     * @return \Application_Model_Account
     */
    public function searchById($id) {
        $select = $this->getDbTable()->select()->where('id_account = ?',(int)$id);
        
        $this->checkPrivileges($select, 'id_account');
        
        
        $result = $select->query(Zend_Db::FETCH_ASSOC)->fetch();
        
        if(is_null($result)) {
            return $result;
        }
        
        return new Application_Model_Account($result);
                
    }
    
    /**
     * Zwraca kolekcję objektów account
     * @return \Application_Model_Collection_Accounts
     */
    public function searchAll() {
                
        $select = $this->getDbTableSelect();        
        $this->checkPrivileges($select, 'a.id_account');        
        $this->_joinPerson($select);       
        
        $result = $select->query(Zend_Db::FETCH_ASSOC)->fetchAll();
        
        return new Application_Model_Collection_Accounts($result);
        
    }
    
    
    
    /**
     * 
     * @param Zend_Db_Table_Select $select
     * @return type
     */
    private function _joinPerson(&$select) {
        
        $select->setIntegrityCheck(false);
        
        $select->joinLeft(array('worker'=>'persons'),'a.id_account = worker.id_person');
        return $select;
    }
    
    protected function getDbTableSelect() {
        return $this->getDbTable()->select()->setIntegrityCheck(false)->from(array('a' => 'accounts'));
    }

    
}

?>
